package com.epam.edu.command;

import com.epam.edu.dao.DAOSpecialty;
import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.Specialty;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class SaveSpecialtyCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, NoSuchAlgorithmException {
        ConnectionPool pool = null;
        Connection connection = null;
        try {
            ConnectionPool.init();
            pool = ConnectionPool.getInstance();
            connection = pool.takeConnection();
            updateSpecialtyCode(connection, request);
            updateSpecialtyName(connection, request);
            addNewSpecialty(connection, request);
            deleteCheckSpecialty(connection, request);
            List<Specialty> specialties = DAOSpecialty.selectAll(connection);
            request.setAttribute("specialties", specialties);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/WEB-INF/jsp/admin/catalogSpecialty.jsp";
    }

    private void addNewSpecialty(Connection connection, HttpServletRequest request) {
        String newCode = request.getParameter("newCode");
        String newName = request.getParameter("newName");
        if (newCode != null && newName != null && !"".equals(newCode) && !"".equals(newName)) {
            int id = DAOSpecialty.add(connection, new Specialty(newCode, newName));
        }
    }

    private void updateSpecialtyName(Connection connection, HttpServletRequest request) {
        String[] changeNames = request.getParameterValues("changeName");
        String[] changeNameId = request.getParameterValues("idName");
        if (changeNames != null && changeNameId != null) {
            for (int i = 0; i < changeNames.length; i++) {
                Specialty specialty = DAOSpecialty.selectById(connection, Integer.parseInt(changeNameId[i]));
                specialty.setName(changeNames[i]);
                if (specialty != null) {
                    DAOSpecialty.update(connection, specialty);
                }
            }
        }
    }

    private void updateSpecialtyCode(Connection connection, HttpServletRequest request) {
        String[] changeCodes = request.getParameterValues("changeCode");
        String[] changeCodeId = request.getParameterValues("idCode");
        if (changeCodes != null && changeCodeId != null) {
            for (int i = 0; i < changeCodes.length; i++) {
                Specialty specialty = DAOSpecialty.selectById(connection, Integer.parseInt(changeCodeId[i]));
                specialty.setCode(changeCodes[i]);
                if (specialty != null) {
                    DAOSpecialty.update(connection, specialty);
                }
            }
        }
    }

    private void deleteCheckSpecialty(Connection connection, HttpServletRequest request) {
        String[] checkboxes = request.getParameterValues("checkbox");
        if (checkboxes != null) {
            for (String s : checkboxes) {
                int id = Integer.parseInt(s);
                Specialty specialty = DAOSpecialty.selectById(connection, id);
                if (specialty != null) {
                    DAOSpecialty.delete(connection, specialty);
                }
            }
        }
    }
}
